package com.uxhao.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uxhao.commonutils.R;
import com.uxhao.commonutils.util.JwtUtil;
import com.uxhao.eduservice.client.OrderClient;
import com.uxhao.eduservice.entity.EduCourse;
import com.uxhao.eduservice.entity.frontvo.CourseFrontInfoVo;
import com.uxhao.eduservice.entity.frontvo.CourseFrontQuery;
import com.uxhao.eduservice.entity.vo.ChapterVo;
import com.uxhao.eduservice.service.EduChapterService;
import com.uxhao.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author uxhao
 * @date 2021/1/20 21:32
 */
@Api(tags={"前台课程接口"})
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    /**
     * 条件查询分页查询课程
     *
     * @param page
     * @param limit
     * @param courseFrontVo
     * @return
     */
    @ApiOperation(value = "课程分页")
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontQuery courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse, courseFrontVo);
        // 返回分页所有数据
        return R.ok().data(map);
    }



    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId
            , HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseFrontInfoVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);

        //远程调用，判断课程是否被购买
        boolean buyCourse = orderClient.isBuyCourse(JwtUtil.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isbuy",buyCourse);
    }

}

